﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Web.Security;

public partial class Pages_Store_ContactUs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        phEmail.Controls.Clear();
        phName.Controls.Clear();
        if (User.Identity.IsAuthenticated)
        {
            phName.Controls.Add(new TextBox()
            {
                Text = User.Identity.Name,
                CssClass = "contact_input",
                Enabled = false
            });
            phEmail.Controls.Add(new TextBox() { 
                Text = Membership.GetUser(User.Identity.Name).Email,
                CssClass = "contact_input",
                Enabled = false
            });
        }
        else
        {
            phName.Controls.Add(new TextBox()
            {
                CssClass = "contact_input"             
            });
            phEmail.Controls.Add(new TextBox()
            {
                CssClass = "contact_input"
            });
        }


    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

        mailMessage.To.Add(new MailAddress(System.Configuration.ConfigurationManager.AppSettings["FNSEmailAddress"]));
        mailMessage.From = new MailAddress(((TextBox)phEmail.Controls[0]).Text);

        mailMessage.Subject = "Message from: "+ ((TextBox)phName.Controls[0]).Text;
        mailMessage.Body = txtBodyMessage.Text;
        mailMessage.IsBodyHtml = true;
        mailMessage.Priority = MailPriority.Normal;

        SmtpClient smtpClient = new SmtpClient();
        smtpClient.EnableSsl = true;
        //var userName = System.Configuration.ConfigurationManager.AppSettings["FromEmailAdress"];
        //var password = System.Configuration.ConfigurationManager.AppSettings["EmailPassword"];
        //smtpClient.Credentials = new System.Net.NetworkCredential(userName, password);

        object userState = mailMessage;

        smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
        try
        {
           smtpClient.SendAsync(mailMessage, userState);
           
        }
        catch (Exception ex)
        {
            lblMessage.Text = "A aparut o eroare, info :" + ex.Message;
        }
        
        
    }

    void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        MailMessage mailMessage = default(MailMessage);

        mailMessage = (MailMessage)e.UserState;

        if ((e.Cancelled))
        {
            
            lblMessage.Text = "Cererea de a trimite email a fost anulata. Adresa=" + mailMessage.To[0].Address;
        }
        if ((e.Error != null))
        {
            lblMessage.Text = "A aparut o eroare, info :" + e.Error.Message;
        }
        else
        {
            Response.RedirectToRoute("Home");
        }
    }


}